package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.api.publics.MapApi;
import com.jzo2o.api.publics.dto.response.LocationResDTO;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.*;
import com.jzo2o.customer.mapper.AddressBookMapper;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 地址薄 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-07-06
 */
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements IAddressBookService {


    @Resource
    private AddressBookMapper addressBookMapper;

    @Resource
    private MapApi mapApi;


    @Override
    public List<AddressBookResDTO> getByUserIdAndCity(Long userId, String city) {

        List<AddressBook> addressBooks = lambdaQuery()
                .eq(AddressBook::getUserId, userId)
                .eq(AddressBook::getCity, city)
                .list();
        if(CollUtils.isEmpty(addressBooks)) {
            return new ArrayList<>();
        }
        return BeanUtils.copyToList(addressBooks, AddressBookResDTO.class);
    }

    @Override
    public void add(AddressBookUpsertReqDTO addressBookUpsertReqDTO) {
        Long aLong = UserContext.currentUserId();
        if (1==addressBookUpsertReqDTO.getIsDefault()) {
            cancelDefault(aLong);
        }
        AddressBook addressBook = BeanUtil.toBean(addressBookUpsertReqDTO, AddressBook.class);
        addressBook.setUserId(aLong);

        String s = addressBookUpsertReqDTO.getProvince() +
                addressBookUpsertReqDTO.getCity() +
                addressBookUpsertReqDTO.getCounty() +
                addressBookUpsertReqDTO.getAddress();
        if (ObjectUtil.isEmpty(addressBookUpsertReqDTO.getLocation())){
            LocationResDTO locationByAddress = mapApi.getLocationByAddress(s);
            String location = locationByAddress.getLocation();
            addressBookUpsertReqDTO.setLocation(location);
        }

        if (StringUtils.isNotEmpty(addressBookUpsertReqDTO.getLocation())){
            addressBook.setLon(NumberUtils.parseDouble(addressBookUpsertReqDTO.getLocation().split(",")[0]));
            addressBook.setLat(NumberUtils.parseDouble(addressBookUpsertReqDTO.getLocation().split(",")[1]));
        }
        addressBookMapper.insert(addressBook);
    }

    @Override
    public PageResult<AddressBookResDTO> page(AddressBookUpsertReqDTO addressBookUpsertReqDTO) {
        Page<AddressBook> addressBookPage = PageUtils.parsePageQuery(addressBookUpsertReqDTO, AddressBook.class);
        LambdaQueryWrapper<AddressBook> eq = Wrappers.<AddressBook>lambdaQuery()
                .eq(AddressBook::getUserId, UserContext.currentUserId());
        Page<AddressBook> addressBookPage1 = addressBookMapper.selectPage(addressBookPage, eq);
        return PageUtils.toPage(addressBookPage1,AddressBookResDTO.class);
    }

    @Override
    public void update(Long id, AddressBookUpsertReqDTO addressBookUpsertReqDTO) {
        if (1==addressBookUpsertReqDTO.getIsDefault()){
            cancelDefault(UserContext.currentUserId());
        }

        AddressBook bean = BeanUtil.toBean(addressBookUpsertReqDTO, AddressBook.class);
        bean.setId(id);

        String s = addressBookUpsertReqDTO.getProvince() +
                addressBookUpsertReqDTO.getCity() +
                addressBookUpsertReqDTO.getCounty() +
                addressBookUpsertReqDTO.getAddress();
        LocationResDTO locationByAddress = mapApi.getLocationByAddress(s);
        String location = locationByAddress.getLocation();
        if (StringUtils.isNotEmpty(location)){
            bean.setLon(NumberUtils.parseDouble(addressBookUpsertReqDTO.getLocation().split(",")[0]));
            bean.setLat(NumberUtils.parseDouble(addressBookUpsertReqDTO.getLocation().split(",")[1]));
        }
        addressBookMapper.updateById(bean);
    }

    @Override
    public void updateDefaultStatus(Long aLong, Long id, Integer flag) {
        if (1==flag){
            cancelDefault(aLong);
        }
        AddressBook addressBook = new AddressBook();
        addressBook.setId(id);
        addressBook.setIsDefault(flag);
        addressBookMapper.updateById(addressBook);
    }

    @Override
    public AddressBookResDTO defaultAddress() {
        LambdaQueryWrapper<AddressBook> eq = Wrappers.<AddressBook>lambdaQuery()
                .eq(AddressBook::getUserId, UserContext.currentUserId())
                .eq(AddressBook::getIsDefault, 1);
        AddressBook addressBook = addressBookMapper.selectOne(eq);
        return BeanUtil.toBean(addressBook,AddressBookResDTO.class);
    }

    private void cancelDefault(Long userId) {
        LambdaUpdateWrapper<AddressBook> updateWrapper = Wrappers.<AddressBook>lambdaUpdate()
                .eq(AddressBook::getUserId, userId)
                .set(AddressBook::getIsDefault, 0);
        super.update(updateWrapper);
    }
}
